package ui
{
	import Base.Loader.SWFLoader;
	
	import Common.RenderManager;
	
	import flash.events.MouseEvent;
	
	import logic.GameItem;
	
	import net.ItemNetHandler;

	public class TeamUIHandler extends uiBase
	{
		public function TeamUIHandler()
		{
			path = "team.swf";
			name = "TeamUI";
		}
				
		
		public override function onInit( loader:SWFLoader ):void
		{
			super.onInit( loader );
			
			uiMc.returnButton.addEventListener( MouseEvent.CLICK , onReturn );
			
			updateData();
			
			for( var i:int ; i < uiMc.creatureArray.length ; i++ )
			{
				uiMc.creatureArray[i].nameButton.addEventListener( MouseEvent.CLICK , onName( i ) );
			}
		}
		
		public override function resize():void
		{
			if ( uiMc )
			{
				uiMc.x = uiMc.px + 15;
				uiMc.y = RenderManager.instance.stageHeight * 0.1 + uiMc.py;
			}
		}
		
		
		public function updateData():void
		{
			if( !uiMc )
			{
				return;
			}
			
			uiMc.clear();
			
			var mainPlayer:GamePlayer = GameManager.instance.MainPlayer;
			var petArray:Array = mainPlayer.petArray;
			
			uiMc.setText( mainPlayer.Name , mainPlayer.data.HP , mainPlayer.data.MAXHP , mainPlayer.data.MP );
			
			for ( var i:int = 0 ; i < petArray.length ; i++ )
			{
				uiMc.setPet( i , petArray[i].Name , petArray[i].data.HP , petArray[i].data.MAXHP , petArray[i].data.MP );
			}
		}
		
		
		public static var instance:TeamUIHandler = null;
		public static function Instance():TeamUIHandler
		{
			if ( instance == null )
			{
				instance = new TeamUIHandler();
			}
			
			return instance;
		}
		
		public function onName( i:int ):Function
		{
			return function foo( e:MouseEvent ):void
			{
				if ( ItemUIHandler.instance.SelectItem )
				{
					var selectI:int = ItemUIHandler.instance.SelectItem.i;
					var mainPlayer:GamePlayer = GameManager.instance.MainPlayer;
					mainPlayer.useItem( selectI , i );
					
					if ( i == 0 )
					{
						ItemNetHandler.sendUseItem( selectI , i , GameItem.UIT_PLAYER );
					}
					else if ( i >= 1 && i <= 5 )
					{
						var petArray:Array = mainPlayer.petArray;
						ItemNetHandler.sendUseItem( selectI , petArray[ i - 1 ].ID , GameItem.UIT_PET );
					}
					else
					{
						
					}
					
					ItemUIHandler.instance.updateSelectItem();
					
					if ( !ItemUIHandler.instance.SelectItem.num )
					{
						ItemUIHandler.instance.clearItem( selectI );
						mainPlayer.clearItem( selectI );
					}
					
					
				}
			}
		}
		
		public function onReturn( e:MouseEvent ):void
		{
			visible( false );
			RoleInfoUIHandler.instance.visible( true );
		}
		
	}
}